/*
 * @Author: zjj
 * @Date: 2019-10-29 12:56:59
 * @LastEditors  : zjj
 * @LastEditTime : 2020-01-16 10:39:47
 */
import Vue from 'vue'
import Vuex from 'vuex'
import router from './router'
//webSocket
import WS from "./utils/websocket"
import GuideDialog from "./components/Guide.js"
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState({
    reducer(val) {
      return {
        activityId: val.activityId,
        isFisrtStart: val.isFisrtStart,
        isLogin: val.isLogin,
        token: val.token,
        location: val.location
      }
    }
  })],
  state: {
    isHandle: false,
    activityId: -1,
    isFisrtStart: false,
    isLogin: false,
    token: "",
    order: "",
    pos: "",
    location: [],
    routes: []
  },
  mutations: {
    addRoute(state, route) {
      state.routes.push(route)
    },
    changeHandle(state) {
      state.isHandle = true
    },
    saveLocation(state, location) {
      let index = state.location.findIndex(item => {
        if (item.province == location.province && item.city == location.city && item.district == location.district) {
          return item
        }
      })
      if (index != -1) {
        state.location.unshift(state.location[index])
        state.location.splice(index + 1, 1)
      }
      else {
        state.location.unshift(location)
      }
    },
    firstStart(state) {
      state.isFisrtStart = true
    },
    changeOrder(state, value) {
      state.order = value
    },
    changePos(state, value) {
      state.pos = value
    },
    saveToken(state, token) {
      if (token) {
        state.isLogin = true
        state.token = token
        // GuideDialog()
        // router.replace({ path: '/index/home' })
        WS.start()
      }
    },
    saveID(state, id) {
      state.activityId = id
    },
    exit(state) {
      state.isLogin = false
      state.token = ""
      state.activityId = -1
      router.replace({ path: '/index/home' })
    },
    exitToLogin(state) {
      state.isLogin = false
      state.token = ""
      state.activityId = -1
    }
  },
  actions: {

  }
})
